package com.ztc.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ztc.bean.LibraryModel;
import com.ztc.bean.ProjectModel;
import com.ztc.dao.LibraryDao;
import com.ztc.dao.ReportDao;

/**
 * @类名：LibraryService.java
 * @作者：李晓平
 * @时间：2018年7月25日 下午3:08:14
 * @描述：行业库service
 */
@Service
public class LibraryService {

	@Autowired
	private LibraryDao libraryDao;
	
	@Autowired
	private ReportDao reportDao;
	
	@Autowired
	private ReportService reportService;
	
	/**
	 * 查询行业库
	 * @param library
	 * @return
	 */
	public List<LibraryModel> queryLibraryList(LibraryModel library) {		
		return libraryDao.queryLibraryList(library);
	}
	
	/**
	 * 查询单个行业库明细
	 * @param id
	 * @return
	 * @throws Exception 
	 */
	public LibraryModel queryOneLibrary(int id, int projectId, int authId) throws Exception {
		LibraryModel libraryModel = libraryDao.queryOneLibrary(id);
		ProjectModel project = reportService.queryProjectById(projectId);
		String newContent = reportService.dealQuoteRule(project, authId, libraryModel.getContent(), 1);
		libraryModel.setContent(newContent);
		return libraryModel;
	}
	
	/**
	 * 添加行业库
	 * @param library
	 * @param commonLibrary
	 * @return
	 */
	@Transactional
	public void addLibrary(LibraryModel library, int commonLibrary) {
		if(commonLibrary == 1) {
			library.setCategoryId(0); //0-通用行业库
		} else {
			//根据项目ID查询行业分类ID
			if(library.getCategoryId() == null) {
				ProjectModel project = reportDao.queryProjectById(library.getProjectId());
				library.setCategoryId(project.getProjectTypeId());
			}
		}
		//添加到行业库
		libraryDao.addLibrary(library);
	}
	
	/**
	 * 删除行业库
	 * @param id
	 */
	@Transactional
	public void deleteLibrary(int id) {
		libraryDao.deleteLibrary(id);
	}
}
